﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using SIPO.Library.DomainObject.Administration.Catalog;
using SIPO.Library.DomainObject.Administration.UserManagement;
using SIPO.Library.DomainObject.Transaction;
using SIPO.Library.DataAccess.Transaction;
using SIPO.Library.DataAccess.Administration.Catalog;
using SIPO.Library.DataAccess.Administration.UserManagement;
using System.Configuration;
using SIPO.Web.Support;
using System.Globalization;


namespace SIPO.Web.Forms.Transaction 
{
    public partial class recObjectionForm : BasePage
    {
        private int id = -1;

        protected override void OnLoad(EventArgs e)
        {

            if (Request.Params["id"] != null)
            {
                id = Convert.ToInt32(Request.Params["id"]);
            }


            if (!Page.IsPostBack)
            {
                recDebtorRepo debtorDal = new recDebtorRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
                var debtors = debtorDal.GetListDebtorSanction();
                ddlDebitur.Items.Clear();
                debtors.ForEach(delegate(recDebtor debtor)
                {
                    ddlDebitur.Items.Add(new ListItem(debtor.DebtorName, debtor.recDebtorSector.DebtorSectorID.ToString()));
                });

                ddlDebitur.Items.Insert(0, new ListItem("-- pilih debitur --", string.Empty));

                if (id > 0)
                {
                    recSanctionRepo recSanctionDal = new recSanctionRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
                    List<recSanction> sanctions = recSanctionDal.GetList();
                    ddlSanksi.Items.Clear();
                    sanctions.ForEach(delegate(recSanction sanction)
                    {
                        ddlSanksi.Items.Add(new ListItem(sanction.LetterNo, sanction.SanctionID.ToString()));
                    });

                    ddlSanksi.Items.Insert(0, new ListItem("-- pilih surat sanksi --", string.Empty));

                    recObjectionRepo objectionDal = new recObjectionRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
                    recObjection objection = objectionDal.GetById(id);

                    if (objection.DebtorSectorID > 0)
                    {
                        var results = debtors.FindAll(x=> x.recDebtorSector.DebtorSectorID == objection.DebtorSectorID);

                        if (results.Count > 0)
                            ddlDebitur.Items.FindByValue(objection.DebtorSectorID.ToString()).Selected = true;
                        else
                        {
                            recDebtorSectorRepo debtorSectorDal = new recDebtorSectorRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
                            var debtorSector = debtorSectorDal.GetById(objection.DebtorSectorID);
                            var debtor = debtorDal.GetById(debtorSector.DebtorID);

                            if (debtor != null)
                            {
                                debtors.Add(debtor);
                                var orderedList = debtors.OrderBy(x => x.DebtorName).ToList();
                                ddlDebitur.Items.Clear();
                                orderedList.ForEach(delegate(recDebtor recdebtor)
                                {
                                    if (recdebtor.recDebtorSector != null)
                                        ddlDebitur.Items.Add(new ListItem(recdebtor.DebtorName, recdebtor.recDebtorSector.DebtorSectorID.ToString()));
                                    else
                                        ddlDebitur.Items.Add(new ListItem(recdebtor.DebtorName, objection.DebtorSectorID.ToString()));

                                });

                                ddlDebitur.Items.Insert(0, new ListItem("-- pilih debitur --", string.Empty));

                                ddlDebitur.Text = objection.DebtorSectorID.ToString();
                            }

                        }

                        var list = sanctions.FindAll(x => x.DebtorSectorID == objection.DebtorSectorID);

                        ddlSanksi.Items.Clear();
                        list.ForEach(delegate(recSanction sanction)
                        {
                            ddlSanksi.Items.Add(new ListItem(sanction.LetterNo, sanction.SanctionID.ToString()));
                        });

                        ddlSanksi.Items.Insert(0, new ListItem("-- pilih surat sanksi --", string.Empty));

                        ddlDebitur.Enabled = false;
                        ddlSanksi.Enabled = false;
                    }

                    hfID.Value = id.ToString();
                    
                    txtNoSurat.Text = objection.ObjectionNo.ToString();
                    txtTglSurat.Text = objection.ObjectionDate.ToString();

                    ddlSanksi.ClearSelection();
                    if (objection.SanctionID.HasValue)
                    {
                        ddlSanksi.Items.FindByValue(objection.SanctionID.Value.ToString()).Selected = true;
                        
                    }

                    if (objection.SanctionValue.HasValue)
                        txtNilaiSanksi.Text = objection.SanctionValue.Value.ToString();
                    
                    txtTglSanksi.Text = objection.SanctionDate.ToString();
                    hfSanctionNo.Value = objection.SanctionNo;
                    hfTransNo.Value = objection.TransNo;
                    hfTransStatus.Value = objection.TransStatus;
                    txtCatatan.Text = objection.ObjectionNote.ToString();
                    hfBillingNo.Value = objection.BillingNo;
                }
            }

            GetPrivilege();

        }

        private void GetPrivilege()
        {
            Membership membership = new Membership(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);

            List<recAccessRights.AccessRights> accessRights = membership.GetAccessRights(base.UserID, base.MenuID);

            accessRights.ForEach(delegate(recAccessRights.AccessRights access)
            {
                switch (access)
                {
                    case (recAccessRights.AccessRights.View):
                        ClientScript.RegisterStartupScript(typeof(Page), "view", "$('#btnSave').hide();", true);
                        break;
                    case (recAccessRights.AccessRights.Add):
                        ClientScript.RegisterStartupScript(typeof(Page), "add", "$('#btnSave').show();", true);
                        break;
                    case (recAccessRights.AccessRights.Edit):
                        ClientScript.RegisterStartupScript(typeof(Page), "edit", "$('#btnSave').show();", true);
                        break;
                    case (recAccessRights.AccessRights.NoAccess):
                        var url = Helper.GetAppVirtualPath(Page);
                        ClientScript.RegisterStartupScript(typeof(Page), "noAccess", "alert('Anda tidak mempunyai hak akses ke halaman ini!'); window.location = '" + url + "'", true);
                        break;
                }
            });

        }


    }
}
